package com.lx.yygh.util;

import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;

import java.util.Map;

/**
 * @author c先生
 * @date 2022年08月09日 下午 7:15:21
 * @describe
 */

public class SyncSmsUtil {
//    public static final String accessKeyId = "LTAI5tH69Qy5D5ACwMmnmFyK";
//    public static final String accessKeySecret = "ZpS7oSa5Ml118JOFlvr49WmukqJ4Xz";
//    public static final String accessKeyId = "LTAI5t6nyVmzuyV3nwmb9R7j";
//    public static final String accessKeySecret = "RlzDpE29xW7XMEwtTElDmRNCoyRiWa";
    //宽的阿里云短信（随便用）
    public static final String accessKeyId = "LTAI5t9gXrbyMk6ytjWWDcMk";
    public static final String accessKeySecret = "xntONgX4lT8ckgioGSlpdOtdmPvPuM";
    public static com.aliyun.dysmsapi20170525.Client createClient() {
        Config config = new Config()
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = "dysmsapi.aliyuncs.com";
        com.aliyun.dysmsapi20170525.Client client = null;
        try {
            client = new com.aliyun.dysmsapi20170525.Client(config);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return client;
    }

    public static boolean sendMessage(Map<String,String> map, String phoneNumber, String templateCode){

        com.aliyun.dysmsapi20170525.Client client = SyncSmsUtil.createClient();
        SendSmsRequest sendSmsRequest = null;
        if("SMS_248430267".equals(templateCode)){
            //发送验证码的模板
            sendSmsRequest = new SendSmsRequest()
                    .setSignName("将年不负")
                    .setTemplateCode(templateCode)
                    .setPhoneNumbers(phoneNumber)
                    .setTemplateParam("{'code':'"+map.get("code")+"'}");
        }else if("SMS_248580228".equals(templateCode)){
            //预约成功的通知模板
            sendSmsRequest = new SendSmsRequest()
                    .setSignName("将年不负")
                    .setTemplateCode(templateCode)
                    .setPhoneNumbers(phoneNumber)
                    .setTemplateParam("{ 'name':'"+map.get("name")+"','time':'"+map.get("time")+"','phone':'"+map.get("phone")+"'}");
        }else if("SMS_248905322".equals(templateCode)){
            //胖胖冬的模板
            sendSmsRequest = new SendSmsRequest()
                    .setSignName("胖胖冬")
                    .setTemplateCode(templateCode)
                    .setPhoneNumbers(phoneNumber)
                    .setTemplateParam("{'code':'"+map.get("code")+"'}");
//                    .setTemplateParam("{ 'name':'"+map.get("name")+"','time':'"+map.get("time")+"','phone':'"+map.get("phone")+"'}");
        }

        RuntimeOptions runtime = new RuntimeOptions();
        try {
            client.sendSmsWithOptions(sendSmsRequest, runtime);
        } catch (TeaException error) {
            error.printStackTrace();
            com.aliyun.teautil.Common.assertAsString(error.message);
            return false;
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            error.printStackTrace();
            com.aliyun.teautil.Common.assertAsString(error.message);
            return false;
        }
        return true;
    }
}
